package com.coocaa.ops.api.config.database;

import com.alibaba.druid.pool.DruidDataSource;
import com.coocaa.ops.api.config.database.properties.DatabaseProperties;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * @author tangminghong
 * @date 2020-06-09 23:42
 */
@Configuration
@MapperScan(basePackages = {"com.coocaa.ops.api.v1.**.mapper"})
public class MybatisConfig {

    @Autowired
    private DatabaseProperties databaseProperties;

    /**
     * 配置数据源
     * 使用@ConfigurationProperties会从配置文件获取并覆盖从databaseProperties获取的配置，
     * 此处nacos手动配置url、用户名、密码，不能在配置文件中再配置，否则将覆盖nacos的配置
     *
     * @return
     */
    @ConfigurationProperties(prefix = "spring.datasource.druid")
    @Bean
    @Primary
    public DruidDataSource druidDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(databaseProperties.getUrl());
        dataSource.setUsername(databaseProperties.getLoginName());
        dataSource.setPassword(databaseProperties.getPassword());
        return dataSource;
    }
}
